YAHOO.namespace("card");
YAHOO.namespace("card.refund");

var Dom = YAHOO.util.Dom,
    Event = YAHOO.util.Event;

YAHOO.card.refund.init = function () {
    var number_per_page_default = 10;
    var paginator = new YAHOO.widget.Paginator({
        containers:['user-list-paging'],
        rowsPerPage:number_per_page_default,
        firstPageLinkLabel:'首页',
        previousPageLinkLabel:'上一页',
        nextPageLinkLabel:'下一页',
        lastPageLinkLabel:'末页'
    });
    var tableConfig = {
        paginator:paginator,
        MSG_EMPTY:'没有记录',
        dynamicData:true,
        initialLoad:false
    };

    var columnDefs = [
        {key:"username", label:'用户名'},
        {key:"deviceNumber", label:'设备号'},
        {key:"source", label:'来源系统'},
        {key:"sourcePk", label:'系统主键'},
        {key:"userOzingPoint", label:'星币余额'},
        {key:"fullName", label:'用户姓名'}
    ];
    var dataSource = new YAHOO.util.DataSource('/ozing/users/json?');
    dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
    dataSource.responseSchema = {
        resultsList:'records',
        fields:['username', 'deviceNumber', 'source', 'sourcePk', 'userOzingPoint', 'fullName'],
        metaFields:{
            totalRecords:'totalRecords',
            paginationRecordOffset:"startIndex",
            paginationRowsPerPage:"pageSize"
        }
    };
    var dataTable = new YAHOO.widget.DataTable("user-list-table", columnDefs, dataSource, tableConfig);

    dataTable.showTableMessage(dataTable.get("MSG_LOADING"), YAHOO.widget.DataTable.CLASS_LOADING);

    var History = YAHOO.util.History;

    var handlePagination = function (state) {
        var newState = state.page;
        History.navigate("page", newState + "");
    };

    paginator.unsubscribe("changeRequest", dataTable.onPaginatorChangeRequest);
    paginator.subscribe("changeRequest", handlePagination, dataTable, true);

    dataTable.doBeforeLoadData = function (oRequest, oResponse, oPayload) {
        var meta = oResponse.meta;
        oPayload.totalRecords = meta.totalRecords || oPayload.totalRecords;
        oPayload.pagination = {
            rowsPerPage:meta.paginationRowsPerPage || number_per_page_default,
            recordOffset:meta.paginationRecordOffset || 0
        };
        return true;
    };

    var generateRequest = function (page) {
        page = page || 1;
        var start_index = (page - 1) * number_per_page_default;
        return "start_index=" + start_index + "&num_per_page=" + number_per_page_default + "&timestamp=" + new Date().getTime();
    };

    var handleHistoryNavigation = function (page) {
        dataTable.getTbodyEl().style.display = "none";
        dataTable.showTableMessage('正在加载中，请稍候...');

        dataSource.sendRequest(generateRequest(page), {
            success:function () {
                dataTable.getTbodyEl().style.display = "";
                dataTable.hideTableMessage();
                dataTable.onDataReturnReplaceRows.apply(this, arguments);
            },
            failure:function () {
                dataTable.getTbodyEl().style.display = "";
                dataTable.hideTableMessage(YAHOO.widget.DataTable.MSG_ERROR);
                dataTable.onDataReturnReplaceRows.apply(this, arguments);
            },
            scope:dataTable,
            argument:{} // Pass in container for population at runtime via doBeforeLoadData
        });
    };

    var initialRequest = History.getBookmarkedState("page") || "1";

    History.register("page", initialRequest, handleHistoryNavigation);

    History.onReady(function () {
        renderPage();
    });

    var renderPage = function () {
        var currentState = History.getCurrentState("page");
        handleHistoryNavigation(currentState);
    };

    // Initialize the Browser History Manager.
    YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");

    return {
        ds:dataSource,
        dt:dataTable
    };

};

Event.onDOMReady(function () {
    YAHOO.card.refund.init();
});






